package com.itheima.sc.service.order;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.sc.api.order.OmsOrderReturnApplyApi;
import com.itheima.sc.pojo.order.OmsOrderReturnApply;
import com.itheima.sc.result.Result;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

@Service
public class OmsOrderReturnApplyService {
    @DubboReference
    OmsOrderReturnApplyApi omsOrderReturnApplyApi;
    OmsOrderUtil omsOrderUtil = new OmsOrderUtil();

    public Result listOmsOrderReturnApply(Integer page, Integer size, Map<String, String> map) {
        //订单列表
        Page<OmsOrderReturnApply> omsOrders = omsOrderReturnApplyApi.listOmsOrderReturnApply(page, size, map);
        PageResult pageResult = new PageResult(page, size, Convert.toInt(omsOrders.getTotal()), Convert.toInt(omsOrders.getPages()), omsOrders.getRecords());
        return Result.success(pageResult);
    }

    public Result deletByIds(ArrayList<Long> ids) {
        if (ObjectUtil.isEmpty(ids) || ids.size() == 0) {
            return Result.error("请先选择要删除的申请");
        }
        Boolean isDelet = omsOrderReturnApplyApi.deletByIds(ids);
        return isDelet ? Result.success("已经删除了") : Result.error("请先处理退货申请");
    }

    //修改退货申请状态
    public Result updateStatus(Long id, Map<String, String> map) {
        Long orderId = omsOrderReturnApplyApi.updateStatus(id, map);
        Integer status = Convert.toInt(map.get("status"));
        if (status ==1){
            return Result.success("已同意退货申请");
        }else if (status==3){
           return Result.success("拒绝退货申请");
        }
        return  Result.success("修改成功") ;
    }

    public Result getById(Long id) {
        OmsOrderReturnApply omsOrderReturnApply=omsOrderReturnApplyApi.getById(id);
        return Result.success(omsOrderReturnApply);
    }
}

